<?php
/**
 * Created by PhpStorm.
 * User: u
 * Date: 2018/12/11
 * Time: 17:46
 */
namespace common\logics\category;

use common\widgets\Tool;
use Yii;
use yii\base\BaseObject;
class CategoryLogic extends BaseObject implements ICategory
{
    /** @var \common\sources\write\category\CategoryAccess $WCategoryAccess **/
    private $WCategoryAccess;

    /** @var \common\sources\read\category\CategoryAccess $RCategoryAccess */
    private $RCategoryAccess;

    public function init()
    {
        $this->WCategoryAccess = Yii::$container->get("WCategoryAccess");
        $this->RCategoryAccess = Yii::$container->get("RCategoryAccess");
        parent::init(); // TODO: Change the autogenerated stub
    }

    public function addCategory($data)
    {
        if(empty($data['name'])){
            return Tool::returnJson([],401,"分类名称不能为空");
        }
        if(!is_numeric($data['pid'])){
            return Tool::returnJson([],402,"父级id有误");
        }
        $result = $this->WCategoryAccess->addCategory($data);
        if($result){
            return Tool::returnJson([],200,"成功");
        }
        return Tool::returnJson([],403,"添加绑定店铺失败");

    }
    //获取父类列表
    public function getList()
    {
        $list = $this->RCategoryAccess->getList();
        return Tool::returnJson($list,200,"成功");
    }
    //获取所有的分类列表
    public function getAll()
    {
        //所有的父类列表
        $list = $this->RCategoryAccess->getList();
        if(count($list) >0 ){
            $arr = array_column($list,'id');
            $ids = implode(',',$arr);
            $cates = $this->RCategoryAccess->getAll($ids);
            $data = [];
            if(count($cates) >0){
                foreach($list as &$item){
                    $item['cate_name'] = '';
                    $data[] = $item;
                    foreach($cates as &$cate){
                        if($item['id'] == $cate['pid'] ){
                            $cat['id'] = $cate['id'];
                            $cat['pid'] = $cate['pid'];
                            $cat['cate_name'] =  $cate['name'];
                            $cat['name'] = $item['name'];
                            $data[] = $cat;
                        }
                    }
                }
                return Tool::returnJson($data,200,"成功");
            }else{
                foreach($list as &$item){
                    $item['name'] = isset($item['name']) ? $item['name'] : '';
                    $item['cate_name']  = '';
                }
                return Tool::returnJson($list,200,"成功");
            }
        }
        return Tool::returnJson($list,200,"成功");
    }
    //修改绑定店铺分类
    public function modifyCategory($data)
    {
        if(empty($data['cate_id'])){
            return Tool::returnJson([],401,"分类id不能为空");
        }
        if(empty($data['cate_name'])){
            return Tool::returnJson([],402,"分类名称不能为空");
        }
        //判断父类级别不能修改问题
        if(!isset($data['pid'])){
            return Tool::returnJson();
        }
        //判断是否有下级分类
        $cates = $this->RCategoryAccess->getAll($data['cate_id']);
//        $category = $this->RCategoryAccess->getCategory($data['cate_id']);
        if(count($cates)>0 && $data['pid'] || ($data['pid'] == $data['cate_id'])){
            return Tool::returnJson([],405,"存在子类目的父类目不能修改成子类目");
        }

        $result = $this->WCategoryAccess->modifyCategory($data);
        if($result){
            return Tool::returnJson([],200,"成功");
        }
        return Tool::returnJson([],403,"修改失败");
    }
    //删除绑定店铺分类
    public function delCategory($data)
    {
        if(empty($data['cate_id'])){
            return Tool::returnJson([],401,"分类id不能为空");
        }
        //判断是否有下级分类
        $cates = $this->RCategoryAccess->getAll($data['cate_id']);
        if(count($cates)>0){
            return Tool::returnJson([],402,"不能删除存在子类级别的父级分类");
        }
        $result = $this->WCategoryAccess->delCategory($data);
        if($result){
            return Tool::returnJson([],200,"成功");
        }
        return Tool::returnJson([],402,"删除失败");

    }
}